package com.youlai.system.plugin.easyexcel;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.youlai.system.model.entity.SysCourse;
import com.youlai.system.model.vo.CoursePeriodImportVO;
import com.youlai.system.service.SysCourseService;
import lombok.extern.slf4j.Slf4j;

/**
 * 故障周期关系批次导入监听器
 */
@Slf4j
public class CoursePeriodImportListener extends MyAnalysisEventListener<CoursePeriodImportVO> {

    private final SysCourseService courseService;

    public CoursePeriodImportListener() {
        courseService = SpringUtil.getBean(SysCourseService.class);
    }

    @Override
    public String getMsg() {
        // 总结信息
        String summaryMsg = StrUtil.format("导入故障周期关系结束：成功{}条，失败{}条；<br/>{}", validCount, invalidCount, msg);
        return summaryMsg;
    }

    @Override
    public void invoke(CoursePeriodImportVO coursePeriodImportVO, AnalysisContext analysisContext) {
        log.info("解析到一条故障周期关系数据:{}", JSONUtil.toJsonStr(coursePeriodImportVO));
        // 校验数据
        StringBuilder validationMsg = new StringBuilder();
        //季度周期处理
        if (StrUtil.isNotBlank(coursePeriodImportVO.getQuarter())) {
            SysCourse course = courseService.getByContentEqual(coursePeriodImportVO.getQuarter());
            if (course != null) {
                course.setMonth(3);
                courseService.updateById(course);
                validCount++;
            } else {
                validationMsg.append(coursePeriodImportVO.getQuarter() + "->故障科目没有找到，无法完成季度周期设置操作; ");
                invalidCount++;
            }
        }
        //半年周期处理
        if (StrUtil.isNotBlank(coursePeriodImportVO.getHalfAYear())) {
            SysCourse course = courseService.getByContentEqual(coursePeriodImportVO.getHalfAYear());
            if (course != null) {
                course.setMonth(6);
                courseService.updateById(course);
                validCount++;
            } else {
                validationMsg.append(coursePeriodImportVO.getHalfAYear() + "->故障科目没有找到，无法完成半年周期设置操作; ");
                invalidCount++;
            }
        }

        //一年周期处理
        if (StrUtil.isNotBlank(coursePeriodImportVO.getAYear())) {
            SysCourse course = courseService.getByContentEqual(coursePeriodImportVO.getAYear());
            if (course != null) {
                course.setMonth(12);
                courseService.updateById(course);
                validCount++;
            } else {
                validationMsg.append(coursePeriodImportVO.getAYear() + "->故障科目没有找到，无法完成一年周期设置操作; ");
                invalidCount++;
            }
        }

        if (!validationMsg.isEmpty()) {
            msg.append("第" + (validCount + invalidCount) + "行数据校验失败：").append(JSONUtil.toJsonStr(coursePeriodImportVO)).append(validationMsg + "<br/>");
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
